package com.nutstudy.mybatis.org.mybatis.spring;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;

import java.util.Set;

/**
 * @author: HyoJoo-W
 * @date: 2021-08-28 13:44
 * @description:
 */


public class NutMapperScanner extends ClassPathBeanDefinitionScanner
{

    public NutMapperScanner(BeanDefinitionRegistry registry)
    {
        super(registry);
    }

    /**
     * 判断某一个组件是否是候选组件
     *
     * @param beanDefinition: BD
     * @return
     */
    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition)
    {
        return beanDefinition.getMetadata().isInterface();
    }

    @Override
    protected Set<BeanDefinitionHolder> doScan(String... basePackages)
    {
        Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);

        for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders)
        {
            BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();

            beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition.getBeanClassName());

            beanDefinition.setBeanClassName(NutFactoryBean.class.getName());
        }

        return beanDefinitionHolders;
    }
}
